#pragma once
#include <iostream>
#include <cstring>

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

class InetAdress
{
    void ToHost()
    {
        _ip = inet_ntoa(_socket.sin_addr); // net to address（IP）
        _port = ntohs(_socket.sin_port);
    }

public:
    InetAdress(const sockaddr_in &socket)
        : _socket(socket)
    {
        ToHost();
    }
    std::string Ip()
    {
        return _ip;
    }
    uint16_t Port()
    {
        return _port;
    }
    bool operator==(InetAdress &right)
    {
        if (_ip == right._ip && _port == right._port) // 一个主机可以有多个用户
        {
            return true;
        }
        return false;
    }
    struct sockaddr_in Socket()
    {
        return _socket;
    }
    std::string Str()
    {
        return _ip + ":" + std::to_string(_port);
    }

private:
    struct sockaddr_in _socket;
    std::string _ip;
    uint16_t _port;
};